package middleware

import (
	"el/app/libs"
	"el/app/service"

	"github.com/labstack/echo/v4"
)

var NoVerifyPath []string = []string{"/admin/login/login"}

func AdminAuth(next echo.HandlerFunc) echo.HandlerFunc {

	return func(c echo.Context) error {
		path := c.Path()
		if libs.InStringArray(path, NoVerifyPath) {
			return next(c)
		}
		Authorization := c.Request().Header.Get("Authorization")
		if Authorization == "" {
			return libs.Error(c, 1001, "请登录！")
		}

		if service.InOutRedis(Authorization) {
			return libs.Error(c, 1003, "请登录！")
		}

		claims, err := libs.ParseToken(Authorization)
		if err != nil {
			return libs.Error(c, 1002, "请登录！")
		}
		c.Set("claims", claims)
		return next(c)
	}
}
